"""
Case Type   : Load_Management
Case Name   : 修改参数io_limits,观察预期结果
Create At   : 2024/10/26
Owner       : liu-tong-8848
Description :
              1.查询io_limits默认值
              2.修改io_limits为5，重启使其生效，并校验其预期结果
              3.重启后做简单DML
              4.清理环境
Expect      :
              1.默认值为0
              2.成功
              3.成功
              4.清理环境成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class SQL(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.com = Common()
        self.constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.db_user_node = Node(node='PrimaryDbUser')

    def test_add_dimension(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        step1 = '--step1:查询io_limits默认值;expect:默认值为0'
        self.log.info(step1)
        result = self.com.show_param("io_limits")
        self.log.info(result)
        self.assertEquals("0", result, "执行失败" + step1)

        step2 = '--step2:修改io_limits为5，重启使其生效，并校验其预期结果;expect:成功'
        self.log.info(step2)
        result = self.primary_sh.execute_gsguc('set',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"io_limits=5")
        self.log.info(result)
        self.assertTrue(result, '执行失败:' + step2)
        self.primary_sh.restart_db_cluster()
        result = self.com.show_param("io_limits")
        self.log.info(result)
        self.assertEquals("5", result, "执行失败" + step2)

        step3 = '--step3:重启后做简单DML;expect:成功'
        self.log.info(step3)
        sql = "drop table if exists t_guc_load_management_0037; create table t_guc_load_management_0037(id int);"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertNotIn("ERROR", result, "执行失败" + step3)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, result, "执行失败" + step3)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, result, "执行失败" + step3)
        sql = "insert into t_guc_load_management_0037 values(generate_series(1,10000));"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertNotIn("ERROR", result, "执行失败" + step3)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, result, "执行失败" + step3)
        sql = "select count(*) from t_guc_load_management_0037;"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertNotIn("ERROR", result, "执行失败" + step3)
        self.assertIn("10000", result, "执行失败" + step3)
        sql = "delete from t_guc_load_management_0037 where id > 1000;"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertNotIn("ERROR", result, "执行失败" + step3)
        self.assertIn(self.constant.DELETE_SUCCESS_MSG, result, "执行失败" + step3)
        sql = "update t_guc_load_management_0037 set id=1000 where id < 10;"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertNotIn("ERROR", result, "执行失败" + step3)
        self.assertIn(self.constant.UPDATE_SUCCESS_MSG, result, "执行失败" + step3)
        sql = "drop table if exists t_guc_load_management_0037;"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertNotIn("ERROR", result, "执行失败" + step3)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, result, "执行失败" + step3)

    def tearDown(self):
        step4 = '--step4:清理环境;expect:清理环境成功'
        self.log.info(step4)
        result = self.primary_sh.execute_gsguc('set',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"io_limits=0")
        self.log.info(result)
        self.assertTrue(result, '执行失败:' + step4)
        self.primary_sh.restart_db_cluster()
        result = self.com.show_param("io_limits")
        self.log.info(result)
        self.assertEquals("0", result, "执行失败" + step4)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
